package com.life.dao.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;


public class GenericManagerDaonalaImpl<T, PK extends Serializable> implements IGenericManager<T, PK> {

	protected final Log log = LogFactory.getLog(getClass());

	private AnnotationSessionFactoryBean sessionFactory;

	protected IGenericDao<T, PK> genericDao;

	public GenericManagerDaonalaImpl() {
	}
 
	public void mergeObj(Object object){
		this.genericDao.mergeObj(object);
	}
	
	public GenericManagerDaonalaImpl(final IGenericDao<T, PK> genericDao) {
		this.genericDao = genericDao;
	}

	public void evict(Object obj) {
		this.genericDao.evict(obj);
	}

	public void clearFlush() {
		this.genericDao.clearFlush();
	}

	public void exceuteSql(String sql) {
		this.genericDao.exceuteSql(sql);
	}
	
	public void update(T object) {
		this.genericDao.update(object);
	}

	public void updateObj(Object obj) {
		this.genericDao.updateObj(obj);
	}

	public void update(List<T> list) {
		this.genericDao.updateList(list);
	}

	@SuppressWarnings("unchecked")
	public void updateList(List list) {
		this.genericDao.updateList(list);
	}

	public void saveObj(Object obj) {
		genericDao.saveObj(obj);
	}
	
	public void saveOrUpdateObj(Object obj) {
    genericDao.saveOrUpdateObj(obj);
  }

	public List<T> getAll() {
		return genericDao.getAll();
	}

	public List<T> getAll(Map<String, Object> equalMap, Map<String, Object> orderMap) {
		return genericDao.getAll(equalMap, orderMap);
	}

	public List<Object> getListByParm(String queryName, Object[] value) {
		return genericDao.getListByParm(queryName, value);
	}

	public T get(PK id) {
		return genericDao.get(id);
	}

	public boolean exists(PK id) {
		return genericDao.exists(id);
	}

	public T save(T object) {
		return genericDao.save(object);
	}

	public void save(List<T> list) {
		for(Object a:list){
			genericDao.saveObj(a);
		}
		
	}

	@SuppressWarnings("unchecked")
	public void saveList(List list) {
		genericDao.saveList(list);
	}

	public void remove(PK id) {
		genericDao.remove(id);
	}
	
	public void remove(T object) {
		genericDao.remove(object);
	}

	public void removeAll(ArrayList<PK> pks) {
		for (Iterator<PK> it = pks.iterator(); it.hasNext();) {
			PK pk = it.next();
			genericDao.remove(pk);
		}
	}

	public void del(Object entity) {
		genericDao.del(entity);
	}

	public void delAll(List entities) {
		genericDao.delAll(entities);
	}

	public void flush() {
		genericDao.flush();
	}


	public int excuteUpdate(String queryName, Map<String, Object> parmMap) {
		return genericDao.excuteUpdate(queryName, parmMap);
	}

	public List<T> getDocAll(Map<String, Object> parmMap) {
		return genericDao.getDocAll(parmMap);
	}

	public List getListByParm(String queryName, Object[] value, Class cls) {
		return genericDao.getListByParm(queryName, value, cls);
	}

	public List getListByParm(String queryName, Object[] value, Class cls, int start, int pagesize) {
		return genericDao.getListByParm(queryName, value, cls, start, pagesize);
	}

	public List getListByParm(String queryName, String[] parms, Object[] value, Class cls) {
		return genericDao.getListByParm(queryName, parms, value, cls);
	}

	public List getListByCriteria(String queryName, String[] parms, Object[] value, Class cls) {
		return genericDao.getListByParm(queryName, parms, value, cls);
	}

	/**
	 * 通过Hibernate criteria 查询;
	 * 
	 * @param criteria
	 * @return
	 */
	public List getListByCriteria(DetachedCriteria criteria) {
		return genericDao.getListByCriteria(criteria);
	}

	public List getListByParm(String queryName, Map<String, Object> pram, Class cls) {
		return genericDao.getListByParm(queryName, pram, cls);
	}

	public List getListByNamedQuery(String namedQuery, Map<String, Object> namedPara) {
		return genericDao.findByNamedQuery(namedQuery, namedPara);
	}

	public List getListByNamedQueryParas(String namedQuery, Map<String, Object> namedPara) {
		return genericDao.getListByNamedQueryParas(namedQuery, namedPara);
	}

	public List getListByNamedQuery(String namedQuery) {
		return genericDao.getListByNamedQuery(namedQuery);
	}

	public List getPageListByParm(String queryName, Map<String, Object> pram,Class cls,  int start, int pagesize) {
		return genericDao.getPageListByParm(queryName, pram,cls, start, pagesize);
	}

	public int excuteUpdateColl(String queryNameStr, Object[] obj) {
		return genericDao.excuteUpdateColl(queryNameStr, obj);
	}

	@SuppressWarnings("unchecked")
	public List getListBySql(String queryName, String wheresql, Class cls) {
		return genericDao.getListBySql(queryName, wheresql, cls);
	}
	@SuppressWarnings("unchecked")
	public List getListBySql(String queryName, String wheresql, Class cls, int start, int pagesize) {
		return genericDao.getListBySql(queryName, wheresql, cls, start, pagesize);
	}

	public AnnotationSessionFactoryBean getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(AnnotationSessionFactoryBean sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public ClassMetadata getClassMetadata(Class<?> clsPo) {
		return genericDao.getClassMetadata(clsPo);
	}

	public String getIdentifierPropertyName(Class<?> clsPo) {
		ClassMetadata clsMeta = genericDao.getClassMetadata(clsPo);
		String idProName = clsMeta.getIdentifierPropertyName();
		return idProName;
	}

	public String getTableName(Class<?> clsPo) {
		AbstractEntityPersister entityPers = genericDao.getEntityPersister(clsPo);
		String tableName = entityPers.getTableName();
		String onlyName = tableName.substring(tableName.indexOf(".") + 1);
		return onlyName;
	}
	
	public void updateBySql(final String sql){
		genericDao.updateBySql(sql);
	}


	@Override
	public Object saveObjSuper(Object obj) {
		// TODO Auto-generated method stub
		return genericDao.saveObjSuper(obj);
	}


	@Override
	public List<Object> getListByNativeSql(String sql, Class cls, int start,
			int pagesize) {
		return genericDao.getListByNativeSql(sql, cls, start, pagesize);
	}
	
	@Override
	public List<Object> getListByNativeSql(String sql , int start,
			int pagesize) {
		return genericDao.getListByNativeSql(sql, start, pagesize);
	}

	public List<Object> getListByNativeSql(String sql){
		return genericDao.getListByNativeSql(sql);
	}
	
	
	/**
     * 查询数据总条数
     * @param sql
     * @return 数据总条数
     * @see [类、类#方法、类#成员]
     */
    public Integer getCountByNativeSql(String sql)
    {
        return genericDao.getCountByNativeSql(sql);
    }

	@Override
	public List<Object> execProc(List inputArgs,List outTypes,String procedureName)
			throws Exception {
		return genericDao.execProc(inputArgs, outTypes, procedureName);
	}

	@Override
	public List<Object> getListByNativeSql(String sql, Class cls) {
		return genericDao.getListByNativeSql(sql, cls);
	}



}

